from django.shortcuts import render
# Create your views here.
from django.views.decorators.cache import cache_page
from time import sleep
from django.core.cache import cache
from django.http import HttpResponse


@cache_page(60)
def show(request):
    data_list = []
    for i in range(10):
        data_list.append(f'第{i}条数据！')
    sleep(30)
    return render(request, 'myapp/show.html', locals())


# 原生方式
def show_old(request):
    data = cache.get('show_old')
    print(data)
    if data:
        return HttpResponse(data)
    data_list = []
    for i in range(10):
        data_list.append(f'第{i}条数据！')
    sleep(10)
    response = render(request, 'myapp/show_old.html', locals())
    cache.set('show_old', response.content, 60)
    return response